﻿namespace HZY.Framework.Core.CustomExceptionHandling;

/// <summary>
/// Return result
/// </summary>
public class R(int code, string? message, object? data)
{
    public object? Data { get; set; } = data;

    public string? ErrorMessage { get; set; } = message;

    public int Code { get; set; } = code;
}

/// <summary>
/// Api message return class
/// Return the ErrorInfoStructure of the interface according to the front-end Umijs specifications
/// </summary>
public static class ApiResult
{
    /// <summary>
    /// Return success
    /// </summary>
    /// <param name="httpStatusCode"> StatusCodes.Status200OK </param>
    /// <param name="message"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    public static R Result(
        HttpStatusCode httpStatusCode,
        string? message,
        object? data)
    {
        return new R((int)httpStatusCode, message, data);
    }

    /// <summary>
    /// Return success
    /// </summary>
    /// <param name="httpContext"></param>
    /// <param name="httpStatusCode"> StatusCodes.Status200OK </param>
    /// <param name="message"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    public static R Result(
        this HttpContext httpContext,
        HttpStatusCode httpStatusCode,
        string? message,
        object? data)
    {
        httpContext.Response.StatusCode = (int)httpStatusCode;

        return new R((int)httpStatusCode, message, data);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="httpStatusCode"> StatusCodes.Status200OK </param>
    /// <param name="message"></param>
    /// <returns></returns>
    public static R ResultMessage(
        HttpStatusCode httpStatusCode,
        string? message)
    {
        return new R((int)httpStatusCode, message, null);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="httpContext"></param>
    /// <param name="httpStatusCode"> StatusCodes.Status200OK </param>
    /// <param name="message"></param>
    /// <returns></returns>
    public static R ResultMessage(
        this HttpContext httpContext,
        HttpStatusCode httpStatusCode,
        string? message)
    {
        httpContext.Response.StatusCode = (int)httpStatusCode;

        return new R((int)httpStatusCode, message, null);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="httpStatusCode"> StatusCodes.Status200OK </param>
    /// <param name="data"></param>
    /// <returns></returns>
    public static R ResultData(
        HttpStatusCode httpStatusCode,
        object? data)
    {
        return new R((int)httpStatusCode, null, data);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="httpContext"></param>
    /// <param name="httpStatusCode"> StatusCodes.Status200OK </param>
    /// <param name="data"></param>
    /// <returns></returns>
    public static R ResultData(
        this HttpContext httpContext,
        HttpStatusCode httpStatusCode,
        object? data)
    {
        httpContext.Response.StatusCode = (int)httpStatusCode;

        return new R((int)httpStatusCode, null, data);
    }

    /// <summary>
    /// Return unauthorized
    /// </summary>
    /// <param name="httpContext"></param>
    /// <param name="message"></param>
    /// <returns></returns>
    public static R ResultUnAuth(this HttpContext httpContext, string? message)
    {
        return httpContext.ResultMessage(HttpStatusCode.Unauthorized, message);
    }
}